//
//  SLAlertView.m
//  SulitLoanDemo
//
//  Created by 虞嘉伟 on 2025/2/21.
//

#import "SLAlertView.h"
#import <YYKit.h>
#import <Masonry.h>


@interface SLAlertView ()
@property (nonatomic, strong) UIView *content;
@property (nonatomic, strong) UIView *pannel;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *closeButton;
@property (nonatomic, strong) UIImageView *decorate;
@property (nonatomic, strong) UILabel *messageLabel;

@property (nonatomic, strong) SLMoreButton *leftButton;
@property (nonatomic, strong) SLMoreButton *rightButton;
@end

@implementation SLAlertView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = UIColorHex(#000000A6);
        
        [self addSubview:self.content];
        [self.content addSubview:self.pannel];
        [self.content addSubview:self.leftButton];
        [self.content addSubview:self.rightButton];
        [self.pannel addSubview:self.titleLabel];
        [self.pannel addSubview:self.closeButton];
        [self.pannel addSubview:self.decorate];
        [self.pannel addSubview:self.messageLabel];
        [self.content mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self).inset(20);
            make.top.equalTo(self).inset(212);
        }];
        [self.pannel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(self.content);
        }];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.pannel).inset(12);
            make.top.equalTo(self.pannel).inset(18);
            make.width.mas_equalTo(self.titleLabel.intrinsicContentSize.width+20*2);
            make.height.mas_equalTo(30);
        }];
        [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.pannel).inset(12);
            make.centerY.equalTo(self.titleLabel);
            make.width.height.mas_equalTo(30);
        }];
        [self.decorate mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.pannel);
            make.top.equalTo(self.titleLabel.mas_bottom).offset(1);
            make.height.mas_equalTo(108); // 依据内容
        }];
        [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.pannel).inset(14);
            make.top.equalTo(self.decorate.mas_bottom).offset(9);
            make.bottom.equalTo(self.pannel).inset(30);
        }];
        
        NSArray *arr = @[self.leftButton, self.rightButton];
        [arr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:23 leadSpacing:15 tailSpacing:15];
        [arr mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.pannel.mas_bottom).offset(22);
            make.height.mas_equalTo(45);
            make.bottom.equalTo(self.content);
        }];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.titleLabel.intrinsicContentSize.width+20*2);
    }];
}

#pragma mark - 点击事件
- (void)closeButtonAction:(UIButton *)button {
    [self removeFromSuperview];
}

- (void)leftButtonAction:(UIButton *)button {
    [self removeFromSuperview];
    !self.leftButtonCallback ?: self.leftButtonCallback(button);
}

- (void)rightButtonAction:(UIButton *)button {
    [self removeFromSuperview];
    !self.rightButtonCallback ?: self.rightButtonCallback(button);
}


- (UIView *)content {
    if (!_content) {
        _content = [UIView new];
    }
    return _content;
}
- (UIView *)pannel {
    if (!_pannel) {
        _pannel = [UIView new];
        _pannel.backgroundColor = UIColor.whiteColor;
        _pannel.layer.cornerRadius = 20;
    }
    return _pannel;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.text = @"Sign out";
        _titleLabel.font = aspectDefaultFont(16);
        _titleLabel.backgroundColor = UIColorHex(#1B1B1B);
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor = UIColor.whiteColor;
        _titleLabel.layer.masksToBounds = YES;
        _titleLabel.layer.cornerRadius = 15;
    }
    return _titleLabel;
}
- (UIButton *)closeButton {
    if (!_closeButton) {
        _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeButton setImage:[UIImage imageNamed:@"icon_close"] forState:UIControlStateNormal];
        [_closeButton addTarget:self action:@selector(closeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeButton;
}
- (UIImageView *)decorate {
    if (!_decorate) {
        _decorate = [UIImageView new];
    }
    return _decorate;
}
- (UILabel *)messageLabel {
    if (!_messageLabel) {
        _messageLabel = [UILabel new];
        _messageLabel.numberOfLines = 0;
        _messageLabel.textAlignment = NSTextAlignmentCenter;
        _messageLabel.font = aspectDefaultFont(14);
        _messageLabel.textColor = UIColorHex(#666666);
        _messageLabel.text = @"Thank you for your use, we have been working hard to provide you with better service!";
    }
    return _messageLabel;
}

- (SLMoreButton *)leftButton {
    if (!_leftButton) {
        _leftButton = [SLMoreButton new];
        _leftButton.titleLabel.text = @"Sign Out";
        _leftButton.imageView.image = [UIImage imageNamed:@"btn_more_black"];
        _leftButton.spacing = 10;
        _leftButton.titleLabel.textColor = UIColorHex(#1B1B1B);
        _leftButton.backgroundColor = UIColor.whiteColor;
        _leftButton.titleLabel.font = aspectDefaultFont(16);
        _leftButton.layer.cornerRadius = 20;
        _leftButton.layer.borderColor = UIColorHex(#1B1B1B).CGColor;
        _leftButton.layer.borderWidth = 1;
        @weakify(self);
        [_leftButton jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
            @strongify(self);
            [self leftButtonAction:nil];
        }];
    }
    return _leftButton;
}
- (SLMoreButton *)rightButton {
    if (!_rightButton) {
        _rightButton = [SLMoreButton new];
        _rightButton.titleLabel.text = @"Leave Behind";
        _rightButton.imageView.image = [UIImage imageNamed:@"btn_more_white"];
        _rightButton.spacing = 10;
        _rightButton.titleLabel.textColor = UIColorHex(#1B1B1B);
        _rightButton.backgroundColor = UIColorHex(#FDC5E0);
        _rightButton.titleLabel.font = aspectDefaultFont(16);
        _rightButton.layer.cornerRadius = 20;
        _rightButton.layer.borderColor = UIColorHex(#1B1B1B).CGColor;
        _rightButton.layer.borderWidth = 1;
        @weakify(self);
        [_rightButton jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
            @strongify(self);
            [self rightButtonAction:nil];
        }];
    }
    return _rightButton;
}


+ (instancetype)detainAlertWithLeftCallback:(void(^)(UIButton *button))leftButtonCallback rightCallback:(void(^)(UIButton *button))rightButtonCallback {
    
    SLAlertView *alert = [SLAlertView new];
    alert.titleLabel.text = @"";
    alert.titleLabel.hidden = YES;
    alert.decorate.image = [UIImage imageNamed:@"alert_sign_out"];
    alert.messageLabel.text = @"Is it because we didn't do something right? Please let us know and we will try to improve!";
    alert.leftButton.titleLabel.text = @"Leave";
    alert.rightButton.titleLabel.text = @"Leave Behind";
    alert.leftButtonCallback = leftButtonCallback;
    alert.rightButtonCallback = rightButtonCallback;
    [UIApplication.sharedApplication.windows.firstObject addSubview:alert];
    [alert mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(alert.superview);
    }];
    
    return alert;
}

+ (instancetype)signOutAlertWithLeftCallback:(void(^)(UIButton *button))leftButtonCallback rightCallback:(void(^)(UIButton *button))rightButtonCallback {
    
    SLAlertView *alert = [SLAlertView new];
    alert.titleLabel.text = @"Sign out";
    alert.decorate.image = [UIImage imageNamed:@"alert_sign_out"];
    alert.messageLabel.text = @"Thank you for your use, we have been working hard to provide you with better service!";
    alert.leftButton.titleLabel.text = @"Sign out";
    alert.rightButton.titleLabel.text = @"Leave Behind";
    alert.leftButtonCallback = leftButtonCallback;
    alert.rightButtonCallback = rightButtonCallback;
    [UIApplication.sharedApplication.windows.firstObject addSubview:alert];
    [alert mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(alert.superview);
    }];
    
    return alert;
}

+ (instancetype)terminateAccountAlertWithLeftCallback:(void(^)(UIButton *button))leftButtonCallback rightCallback:(void(^)(UIButton *button))rightButtonCallback {
    
    SLAlertView *alert = [SLAlertView new];
    alert.titleLabel.text = @"Terminate the account";
    alert.decorate.image = [UIImage imageNamed:@"alert_terminate"];
    alert.messageLabel.text = @"Thank you for your company! After you cancel your account, you will no longer be able to use our services and all your data will be permanently deleted.";
    alert.leftButton.titleLabel.text = @"Terminate";
    alert.rightButton.titleLabel.text = @"Leave Behind";
    alert.leftButtonCallback = leftButtonCallback;
    alert.rightButtonCallback = rightButtonCallback;
    [UIApplication.sharedApplication.windows.firstObject addSubview:alert];
    [alert mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(alert.superview);
    }];
    
    return alert;
}
@end
